function addAlbum(){
	var newAlbumName = document.getElementById('newAlbumName').value;
	if(newAlbumName == '') {
		alert('请输入新建的相册名！');
		document.getElementById('newAlbumName').focus();
		return;
	}
	if(newAlbumName.replace(/[^\x00-\xff]/g,"**").length > 50) {
		alert('相册名不能超过50字符！');
		document.getElementById('newAlbumName').focus();
		return;
	}
	//value= new相册名
	document.getElementById('albumid').options.add(new Option(newAlbumName,'new:' + newAlbumName));
	document.getElementById('albumid').selectedIndex  = document.getElementById('albumid').options.length - 1;
	
	document.getElementById('addAlbum').style.display = 'none';
	document.getElementById('newAlbumName').value = '';
}
function cancelAddAlbum(){
	document.getElementById('addAlbum').style.display = 'none';
	document.getElementById('newAlbumName').value = '';
	document.getElementById('album').selectedIndex  = 0;
}
function changeAlbum(obj){
	if(obj.value == '-1') {
		document.getElementById('addAlbum').style.display = '';
	}else{
		document.getElementById('addAlbum').style.display = 'none';
		document.getElementById('newAlbumName').value = '';
	}
}
function createViewPhotoHtml(picid, picurl) {
	if(picurl == null || picurl == '') {
		return;
	}
	if(picid == null || picid == '') {
		return;
	}
	var temphtml = '<input type="hidden" name="picid" value="' + picid + '"/>';
	temphtml += '<dl class="fieldset">';
	temphtml += '	<dd><img src="' + picurl + '"/></dd>';
	temphtml += '	<dt><label>描述：</label> </dt>';
	temphtml += '	<dd><textarea name="photoDesc'+picid+'" rows="3" cols="20" class="text photoDesc"/></textarea> </dd>';
	temphtml += '</dl>';
	document.getElementById('albumList').innerHTML = document.getElementById('albumList').innerHTML + temphtml;
}
function addErrorMessage(msg) {
	if(document.getElementById('message').innerText == '') {
		document.getElementById('message').innerHTML = '<strong>上传文件发生错误:</strong>';
		document.getElementById('message').style.display = '';
	}
	document.getElementById('message').innerHTML = document.getElementById('message').innerHTML + '<br/>' + msg;
}

function cancelInputPicDescription(uid){
	window.location = 'space.html?uid='+uid+'&do=album&id=' + document.getElementById('uploadAlbumid').value;
}

/**
 * FancyUpload Showcase
 *
 * @license		MIT License
 * @author		Harald Kirschner <mail [at] digitarald [dot] de>
 * @copyright	Authors
 */
window.addEvent('domready', function() { // wait for the content
	MooTools.lang.setLanguage('cnCHS');
	MooTools.lang.set('cnCHS', 'FancyUpload', {"fileName": '{name}'});
	// our uploader instance 
	
	var uploadFileCount = 0;
	var up = new FancyUpload2($('demo-status'), $('demo-list'), { // options object
		// we console.log infos, remove that in production!!
		verbose: true,
		
		path: 'js/fancyupload/Swiff.Uploader.swf',
		
		// url is read from the form, so you just have to change one place
		url: $('form-demo').action,
		
		// remove that line to select all files, or edit it, add more items
		typeFilter: {
			'Images (*.jpg, *.jpeg, *.gif, *.png)': '*.jpg; *.jpeg; *.gif; *.png'
		},
		
		// this is our browse button, *target* is overlayed with the Flash movie
		target: 'demo-browse',

		fieldName : 'attach',
		
		fileSizeMax : 1024*1024*10, //10M 
		
		// graceful degradation, onLoad is only called if all went well with Flash
		onLoad: function() {
			$('demo-status').removeClass('hide'); // we show the actual UI
			//$('demo-fallback').destroy(); // ... and hide the plain form
			
			// We relay the interactions with the overlayed flash to the link
			this.target.addEvents({
				click: function() {
					return false;
				},
				mouseenter: function() {
					this.addClass('hover');
				},
				mouseleave: function() {
					this.removeClass('hover');
					this.blur();
				},
				mousedown: function() {
					this.focus();
				}
			});

			// Interactions for the 2 other buttons
			$('demo-clear').addEvent('click', function() {
				up.remove(); // remove all files
				return false;
			});

			$('demo-upload').addEvent('click', function() {
				if(up.fileList.length == 0) {
					alert("请选择要上传的文件！");
					return false;
				}
				var albumid = document.getElementById('albumid').value;
				if(albumid == '' || albumid == '-1') {
					if(!confirm('您没有选择上传的相册，确认继续进行吗？系统将会默认生成一个相册！')) {
						return false;
					}
				}
				var uid = document.getElementById('curUid').value;
				var url = "do_swfupload.html?albumid=" + escape(albumid) + "&uid=" + uid;
				up.setOptions({url : encodeURI(url)});
				up.start(); // start upload
				return false;
			});
		},
		
		// Edit the following lines, it is your custom event handling
		
		/**
		 * Is called when files were not added, "files" is an array of invalid File classes.
		 * 
		 * This example creates a list of error elements directly in the file list, which
		 * hide on click.
		 */ 
		onSelectFail: function(files) {
			files.each(function(file) {
				new Element('li', {
					'class': 'validation-error',
					html: file.validationErrorMessage || file.validationError,
					title: MooTools.lang.get('FancyUpload', 'removeTitle'),
					events: {
						click: function() {
							this.destroy();
						}
					}
				}).inject(this.list, 'top');
			}, this);
		},
		
		/**
		 * This one was directly in FancyUpload2 before, the event makes it
		 * easier for you, to add your own response handling (you probably want
		 * to send something else than JSON or different items).
		 */
		onFileSuccess: function(file, response) {
		    uploadFileCount ++;
		    if(up.fileList.length == uploadFileCount) {
				//上传完成
				document.getElementById('batchUploadAlbum').style.display = 'none';
				document.getElementById('albumPreview').style.display = '';
			}
			var json = new Hash(JSON.decode(response, true) || {});
			document.getElementById('uploadAlbumid').value = json.get('albumid');
			if (json.get('status') == '1') {
				createViewPhotoHtml(json.get('picid'), json.get('picurl'));
			} else {
				addErrorMessage(json.get('error'));
			}
		},
		
		onFileError: function(file) {
			uploadFileCount ++;
		},
		
		/**
		 * onFail is called when the Flash movie got bashed by some browser plugin
		 * like Adblock or Flashblock.
		 */
		onFail: function(error) {
			uploadFileCount ++;
			switch (error) {
				case 'hidden': // works after enabling the movie and clicking refresh
					alert('To enable the embedded uploader, unblock it in your browser and refresh (see Adblock).');
					break;
				case 'blocked': // This no *full* fail, it works after the user clicks the button
					alert('To enable the embedded uploader, enable the blocked Flash movie (see Flashblock).');
					break;
				case 'empty': // Oh oh, wrong path
					alert('请求的文件不存在，很可能是路径有误或文件已被删除.');
					break;
				case 'flash': // no flash 9+ :(
					alert('当前控件不可用，请安装最新的Flash插件.')
			}
		}
		
	});
	
});